<?php

namespace App\Http\Middleware;

use App\Models\Application;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class CheckApplicationHeader
{
    public function handle(Request $request, Closure $next): Response
    {
        $appKey = $request->header('AppKey');
        $application = Application::where('app_key', $appKey)->first();
        if ($application && $application->isEnabled()) {
            $request->merge([
                'application' => $application,
            ]);

            return $next($request);
        } else {
            abort(404, '应用不存在');
        }
    }
}
